<style></style>

<div id="orderExistsChecker">
    <!-- 检查订单按钮 -->
    <button class="layui-btn layui-btn-normal" id="checkOrderBtn">检查订单是否存在</button>
</div>

<!-- 订单检查模态框 -->
<div id="orderCheckModal" style="display: none; padding: 20px;">
    <form class="layui-form" lay-filter="orderCheckForm">
        <div class="layui-form-item">
            <label class="layui-form-label">订单ID</label>
            <div class="layui-input-block">
                <input type="text" name="orderId" id="orderId" lay-verify="required" placeholder="请输入订单ID" autocomplete="off" class="layui-input">
            </div>
        </div>
        
        <div class="layui-form-item" style="text-align: center;">
            <button class="layui-btn" lay-submit lay-filter="submitCheck">检查订单</button>
            <button type="button" class="layui-btn layui-btn-primary" onclick="closeOrderCheckModal()">取消</button>
        </div>
    </form>
</div>

<script>
    // 关闭订单检查模态框
    function closeOrderCheckModal() {
        layer.closeAll();
    }
</script>

<script>
    layui.use(['layer', 'form'], function () {
        var $ = layui.$;
        var layer = layui.layer;
        var form = layui.form;
        
        /**
         * 打开订单检查模态框
         */
        $(document).on('click', '#checkOrderBtn', function() {
            // 打开模态框
            layer.open({
                type: 1,
                title: '检查订单是否存在',
                content: $('#orderCheckModal'),
                area: ['400px', 'auto'],
                success: function() {
                    // 重新渲染表单
                    layui.form.render();
                    // 清空输入框
                    $('#orderId').val('');
                },
                end: function() {
                    // 隐藏模态框
                    $('#orderCheckModal').hide();
                }
            });
        });
        
        /**
         * 检查订单是否存在
         * @param {string} orderId - 订单ID
         * @returns {Promise} - 返回检查结果的Promise
         */
        function checkOrderExists(orderId) {
            // 显示加载中
            layer.load();
            
            // 调用检查订单是否存在的API
            return $.ajax({
                url: '/api/dailiantong/check_order_exists/',
                type: 'GET',
                data: {
                    'order_id': orderId
                },
                dataType: 'json',
                success: function(res) {
                    // 隐藏加载中
                    layer.closeAll('loading');
                    
                    if (res.code === 0) {
                        layer.msg('订单存在', {icon: 6});
                        return {exists: true, message: res.message};
                    } else {
                        layer.msg('订单不存在', {icon: 5});
                        return {exists: false, message: res.message};
                    }
                },
                error: function(xhr, status, error) {
                    // 隐藏加载中
                    layer.closeAll('loading');
                    
                    layer.msg('网络错误，无法检查订单', {icon: 5});
                    console.error('检查订单失败：', error);
                    return {exists: false, message: '网络错误' + (error ? ': ' + error : '')};
                }
            });
        }
        
        // 表单提交处理
        form.on('submit(submitCheck)', function(data) {
            // 获取表单数据
            var formData = data.field;
            var orderId = formData.orderId;
            
            // 验证订单ID是否为空
            if (!orderId || orderId.trim() === '') {
                layer.msg('请输入订单ID', {icon: 5});
                return false;
            }
            
            // 检查订单是否存在
            checkOrderExists(orderId);
            
            return false; // 阻止表单跳转
        });
        
        // 导出检查订单函数，以便其他页面调用
        window.checkOrderExists = checkOrderExists;
    });
</script>